<?php
/**
 * Created by PhpStorm.
 * User: aako
 * Date: 15-12-8
 * Time: 下午5:41
 */

namespace app\Http\Controllers\Admin;

use App\Models\Brand;
use App\Models\Goods;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Brand\StoreRequest;
use App\Http\Requests\Admin\Brand\UpdateRequest;
use App\Models\Category;
use App\Models\CategoryBrand;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;
use Illuminate\Support\Facades\Input;
use App\Commands\OperationLogCommand;


class BrandController extends Controller
{
    public function brand_gate()
    {
        if (!Gate::allows('brand','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    public function index(Request $request)
    {
        $this->brand_gate();

        $search = array('brand_name'=>'');

        $brands = Brand::orderBy('brand_id','desc');
        if (!empty($request->get('brand_name'))){
            $brands = $brands->where('brand_name', 'like', "%".trim($request->get('brand_name'))."%");
            $search['brand_name'] = $request->brand_name;
        }//end if()
        $brands = $brands->paginate(10);


        return view('admin.brand.index', compact(['brands', 'search']));
    }

    public function create(Brand $brand, Category $category)
    {
        $this->brand_gate();

        return view('admin.brand.create', compact(['brand', 'category']));
    }

    public function store(StoreRequest $storeRequest, Brand $brand)
    {
        $this->brand_gate();

        $brand = $brand->create($storeRequest->all());

        $this->dispatch(
            new OperationLogCommand('添加品牌','Brand','brand_id:'.$brand->brand_id)
        );

        /*$brand_id = $brand->brand_id;
        $cat_id = $storeRequest->cat_id;
        $categorBrand = CategoryBrand::insertGetId(['cat_id'=>$cat_id, 'brand_id'=>$brand_id]);*/

        return Redirect('admin/brand');
    }

    /**
     * 获取品牌
     *
     * @date 2016/04/19
     * @author Leen
     */
    public function show($id)
    {

        $this->brand_gate();

        $brands = Brand::orderBy('brand_id','desc');

        $brands = $brands->get();

        return $brands;
    }//end func show()


    public function edit($brand_id, Category $category)
    {
        $this->brand_gate();

        $brand = Brand::find($brand_id);

        return view('admin.brand.edit', compact('brand', 'category'));
    }

    public function update($brand_id, UpdateRequest $updateRequest)
    {
        $this->brand_gate();

        $brand = Brand::find($brand_id);
        $brand->update($updateRequest->all());
        $this->dispatch(
            new OperationLogCommand('修改品牌','Brand','brand_id:'.$brand_id)
        );

        /*$brand_id = $brand->brand_id;
        $cat_id = $updateRequest->cat_id;
        $categorBrand = CategoryBrand::insertGetId(['cat_id'=>$cat_id, 'brand_id'=>$brand_id]);*/

        return Redirect($updateRequest->get('back_url'));
    }

    public function destroy($brand_id)
    {
        $this->brand_gate();

        $goods = Goods::where('brand_id', $brand_id)->first();

        if(empty($goods)){

            $brand = Brand::find($brand_id);
            $brand->delete();
            $this->dispatch(
                new OperationLogCommand('删除品牌','Brand','brand_id:'.$brand_id)
            );

            $category_brand = CategoryBrand::where('brand_id', $brand_id);
            $category_brand->delete();
            return response()->json(['status'=>200]);

        }else{
            return response()->json(['message'=>'该品牌关联商品']);
        }

        //return Redirect('admin/brand');
    }
    public function toggleShow(){

        $this->brand_gate();

        $id = Input::get("id");

        $is_recommend = Brand::where("brand_id",$id)->pluck("is_recommend");
        $data = array();

        if($is_recommend==0){
            $data['is_recommend'] = 1;
            $data['id'] = $id;
            $res = Brand::where("brand_id",$id)->update(['is_recommend'=>1]);
            $catres = CategoryBrand::where("brand_id",$id)->first()?CategoryBrand::where("brand_id",$id)->update(['is_recommend'=>1]):true;
        }else{
            $data['is_recommend'] = 0;
            $data['id'] = $id;
            $res = Brand::where("brand_id",$id)->update(['is_recommend'=>0]);
            $catres = CategoryBrand::where("brand_id",$id)->first()?CategoryBrand::where("brand_id",$id)->update(['is_recommend'=>0]):true;
        }
        if($res != false && $catres != false){
            $data['info'] = true;
        }else{
            $data['info'] = false;
        }
        return $data;

    }
}